using UnityEngine;
using UnityEngine.U2D.IK;

public class Bow : MonoBehaviour
{
	public Weapon weapon;

	public IKManager2D ikManager;

	public bool onUsing;

	public float penetrationPlayerScaler;

	public float damagePlayerScaler;

	public float stoppingPowerPlayerScaler;

	public AudioClip drawStringLight;

	public AudioClip drawStringHard;

	public AudioClip releaseStringLight;

	public AudioClip releaseStringHard;

	[HideInInspector]
	public GameObject body;

	[HideInInspector]
	public float penetration;

	[HideInInspector]
	public float damage;

	[HideInInspector]
	public float stoppingPower;

	private Transform stringIK;

	private Vector2 defaultStringIKLocalPos;

	private void Start()
	{
		if (Player.Instance.weapon != weapon)
		{
			ChangeDown();
		}
		penetration = weapon.penetration;
		damage = weapon.damage;
		stoppingPower = weapon.stoppingPower;
		body = base.transform.Find("Body").gameObject;
		stringIK = base.transform.Find("Body/StringIK");
		defaultStringIKLocalPos = stringIK.localPosition;
	}

	private void Update()
	{
		if (!onUsing && Player.Instance.weapon == weapon && !Player.Instance.releaseRightHand)
		{
			onUsing = true;
			ChangeUp();
		}
		if (onUsing && (Player.Instance.weapon != weapon || Player.Instance.releaseRightHand))
		{
			onUsing = false;
			ChangeDown();
		}
		if (onUsing && weapon.ammo != 0)
		{
			stringIK.position = weapon.rightHandHandler.transform.position;
		}
		else
		{
			stringIK.localPosition = defaultStringIKLocalPos;
		}
	}

	private void LateUpdate()
	{
		if (!(Player.Instance.weapon == weapon))
		{
			return;
		}
		if (!weapon.onReload && !Player.Instance.releaseRightHand)
		{
			if (!Player.Instance.isPlacingItem)
			{
				Player.Instance.body.rightArm.transform.rotation = Player.Instance.body.leftArm.transform.rotation * Quaternion.Euler(0f, 0f, -190f);
				Player.Instance.body.rightForearm.transform.rotation *= Quaternion.Euler(0f, 0f, 20f);
				Player.Instance.body.rightHand.transform.rotation = weapon.rightHandHandler.transform.rotation * Quaternion.Euler(0f, 0f, 80f);
			}
			else
			{
				Transform obj = Player.Instance.body.rightHand.transform;
				Quaternion localRotation = (Player.Instance.body.rightArm.transform.localRotation = Quaternion.Euler(0f, 0f, 0f));
				obj.localRotation = localRotation;
			}
		}
		else
		{
			Player.Instance.body.rightHand.transform.localRotation = Quaternion.Euler(0f, 0f, -30f);
		}
		if (weapon.ammo == 0 && !weapon.onReload)
		{
			weapon.Reload();
		}
		if (Player.Instance.isPlacingItem && body.activeSelf)
		{
			body.transform.localPosition = Vector2.zero;
			weapon.ammo = 0;
			weapon.magazine.gameObject.SetActive(value: false);
		}
		else if (!Player.Instance.isPlacingItem && !body.activeSelf)
		{
			weapon.magazine.gameObject.SetActive(value: true);
		}
	}

	private void ChangeUp()
	{
		weapon.magazine.gameObject.SetActive(value: true);
	}

	private void ChangeDown()
	{
		weapon.ammo = 0;
		weapon.magazine.gameObject.SetActive(value: false);
	}

	public void PlayDrawStingAudio()
	{
		if (Player.Instance.strength < 4f)
		{
			weapon.audioSource.PlayOneShot(drawStringLight);
		}
		else
		{
			weapon.audioSource.PlayOneShot(drawStringHard);
		}
	}

	public void PlayReleaseStingAudio()
	{
		if (Player.Instance.strength < 4f)
		{
			weapon.audioSource.PlayOneShot(releaseStringLight);
		}
		else
		{
			weapon.audioSource.PlayOneShot(releaseStringHard);
		}
	}
}
